﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.BusinessLogic;
using Module.Core.Common;
using Module.Core.EntitiesInfo;
using Module.Core.LoginSystem.Commons.Framwork;

namespace Web.Cms.ChuyenGold
{


    public partial class LstChuyenGoldATM : AdminUserControl
    {
        #region Khai báo biến
        private int totalRecords;
        private int cPage = 1;
        private DateTime dateTo;
        private DateTime dateFrom;
        private int type;
        private string codememberTo, codeMemberForm;
        private int id;
        private LogChuyenGoldInfo logInfo;
        private MemberInfo infoMemberFrom;
        private MemberInfo infoMemberTo;
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region OnInit
        protected override void OnInit(EventArgs e)
        {
            try
            {


            }
            catch (Exception ex)
            {
                lblMessage.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region OnPreRender
        protected override void OnPreRender(EventArgs e)
        {
            try
            {
                base.OnPreRender(e);
                
                if (Request.QueryString["CodememberForm"] == "" || Request.QueryString["CodememberForm"] == null)
                {
                    txtCodememberForm.Text = "";
                }
                else
                {
                    codeMemberForm = Request.QueryString["CodememberForm"];
                    txtCodememberForm.Text = codeMemberForm;
                }

                if (Request.QueryString["CodememberTo"] == "" || Request.QueryString["CodememberTo"] == null)
                {
                    txtCodememberTo.Text = "";
                }
                else
                {
                    codememberTo = Request.QueryString["CodememberTo"];
                    txtCodememberTo.Text = codememberTo;
                }

                if (Request.QueryString["dateFrom"] == null && Request.QueryString["dateFrom"] == "")
                {
                    dateFrom = Convert.ToDateTime(Request.QueryString["dateFrom"].Trim());
                    txtDateFrom.Text = dateFrom.ToString("dd/MM/yyyy");
                }
                else
                {
                    dateFrom = DateTime.Now.AddMonths(-2);
                    txtDateFrom.Text = dateFrom.ToString("dd/MM/yyyy");
                }

                if (Request.QueryString["dateTo"] == null && Request.QueryString["dateTo"] == "")
                {
                    dateTo = Convert.ToDateTime(Request.QueryString["dateTo"].Trim());
                    txtDateTo.Text = dateTo.ToString("dd/MM/yyyy");
                }
                else
                {
                    dateTo = DateTime.Now.AddMonths(+1);
                    txtDateTo.Text = dateTo.ToString("dd/MM/yyyy");
                }
                LoadData();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }

        }
        #endregion


        #region LoadData

        private void LoadData()
        {
            totalRecords = ChuyenGoldBL.SelectCount(status,2,txtCodememberTo.Text, txtCodememberForm.Text, dateFrom.ToString(), dateTo.AddDays(+1).ToString());
            lbTotal.Text = "Tổng số bản ghi: " + totalRecords;
            if (Request.QueryString.Get("cpage") != null)
                cPage = ObjConvertUtility.ToInt32(Request.QueryString.Get("cPage"));
            if (cPage == 0) cPage = 1;
            int from = (cPage - 1) * Const.pageSize20 + 1;
            if (from == 0) from = 1;
            int to = (cPage) * Const.pageSize20;

            PagingAjax1.totalRecords = totalRecords;
            PagingAjax1.numPerPage = Const.pageSize20;
            PagingAjax1.queryParam = ObjMiscUtility.GetCPPage(CurrentCommand.CtrlKey,
                                    new string[] { "CodememberForm", "CodememberTo", "from", "to", "cPage" },
                                    new string[] {  codeMemberForm, codememberTo, from.ToString(), to.ToString(), "" });

            PagingAjax1.defaultQueryParam = ObjMiscUtility.GetCPPage(CurrentCommand.CtrlKey,
                                    new string[] { "from", "to" },
                                    new string[] { from.ToString(), to.ToString() });
            string order = null;
            DataTable dt;
            dt = ChuyenGoldBL.Search(status,2,txtCodememberTo.Text, txtCodememberForm.Text, dateFrom.ToString(), dateTo.AddDays(+1).ToString(), from, to, order, false);
            if (dt != null && dt.Rows.Count > 0)
            {

                grdChuyenGoldATM.DataSource = dt;
                grdChuyenGoldATM.RowDataBound += new GridViewRowEventHandler(grdChuyenGoldATM_RowDataBound);
                grdChuyenGoldATM.DataBind();

            }

        }
        #endregion
        protected void grdChuyenGoldATM_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                ImageButton btnStatus = (ImageButton)e.Row.FindControl("btnStatus");
                Label lblStatus = (Label)e.Row.FindControl("lblStatus");
                e.Row.Cells[0].Text = (Const.pageSize20 * (cPage - 1) + e.Row.DataItemIndex + 1).ToString();
                if (status == 2)
                {
                    lblStatus.Text = "Chờ xác nhận";
                    btnStatus.ImageUrl = "/App_Themes/Default/Images/Buttons/btn_forward.gif";
                    btnStatus.OnClientClick = "return confirm('Bạn có xác nhận chuyển gold qua ATM này không ?')";
                   
                }
                else if (status == 1)
                {
                    lblStatus.Text = "Đã xác nhận";
                    btnStatus.ImageUrl = "/App_Themes/Default/Images/Buttons/btn_pending.gif";

                }
            }

        }

        protected void grdChuyenGoldATM_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                switch (e.CommandName)
                {
                    case "CommandStatus":
                        try

                        {
                            id = Convert.ToInt32(e.CommandArgument);
                            ChuyenGoldInfo info = ChuyenGoldBL.Select(id);
                            infoMemberFrom = MemberBL.Select(info.CodeMember_From,6);
                            infoMemberTo = MemberBL.Select(info.CodeMember_To, 6);
                            #region khi admin xác nhận thì insert 1 thông báo cho thành viên biết là đã chuyển gold thành công
                            LogChuyenGoldInfo logChuyenGoldInfo=new LogChuyenGoldInfo();
                            logChuyenGoldInfo.CreateDate = DateTime.Now;
                            logChuyenGoldInfo.EmailTransfer = infoMemberFrom.Email;
                            logChuyenGoldInfo.EmailGet = infoMemberTo.Email;
                            logChuyenGoldInfo.CodeMember_From = info.CodeMember_From;
                            logChuyenGoldInfo.CodeMember_To = info.CodeMember_To;
                            logChuyenGoldInfo.Description = "Chúng tôi đã xác nhận giao dịch chuyển gold của bạn.Thành viên "+infoMemberFrom.FullName+" đã chuyển "+info.Gold+ " gold thành công vào số tài khoản "+info.NumberAcount+".Chủ tài khoản là "+info.NameAcount;
                            LogChuyenGoldBL.Insert1(logChuyenGoldInfo);
                            #endregion

                            #region update trạng thái và trừ số gold của người chuyển, Status=2 chờ xác nhận,Status=1 đã xác nhận
                            if (status == 2)// Chuyển gold qua ATM cuả bạn đang chờ duyệt->Status=2
                                {
                              
                                 ChuyenGoldBL.UpdateStatus(id, 1);//Sau khi update Status=1 Chuyển gold qua ATM của bạn đã đc duyệt 
                                 MemberBL.UpdateGold(1,info.CodeMember_From,info.CodeMember_To,info.Gold);
                                }
                            #endregion
                        }
                        catch (Exception ex)
                        {
                            lblMessage.Text = ex.Message;
                        }
                        break;

                    case "Commanddelete":
                        try
                        {
                            int Id = Convert.ToInt32(e.CommandArgument);
                            ChuyenGoldBL.Delete(Id);
                            LoadData();
                        }
                        catch (Exception ex)
                        {
                            lblMessage.Text = ex.Message;
                        }
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }

        private int status;
        public int Status
        {
            set { status = value; }
            get { return status; }
        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?ctrlkey=" + CurrentCommand.CtrlKey +  "&CodememberTo=" + txtCodememberTo.Text.Trim() + "&CodememberForm=" + txtCodememberForm.Text.Trim() + "&dateFrom=" +
                                txtDateFrom.Text.Trim() + "&dateTo=" + txtDateTo.Text.Trim() + "&cPage=" + cPage);
        }
    }
}